import Config from './config.js'
import $store from '@/store/index.js'
import Http from './http.js'
import sdkMethods from './sdkMethods'
// #ifdef H5
import {
	getEnv
} from './env.js'
var envArr = getEnv()
// #endif
import h5Copy from '@/common/js/plug/copy_sdk.js'
export default {
	/**
     * 页面跳转
     * @param {string} to 跳转链接 /pages/index/index
     * @param {Object} param 参数 {key : value, ...}
     * @param {string} mode 模式
     */
	redirectTo(to, param, mode) {
		console.log('this.isNativeApp()this.isNativeApp()this.isNativeApp()', to)

		let url = to
		// console.log({"$store":store.state.$store.tabbarList.list})
		let tabbarList = ['/pages/index/index/index', '/pages/enterprisePurchase/index', '/pages/discover/discover', '/pages/goods/cart/cart', '/pages/member/index/index']

		if (param != undefined) {
			Object.keys(param).forEach((key) => {
				if (url.indexOf('?') != -1) {
					url += '&' + key + '=' + param[key]
				} else {
					url += '?' + key + '=' + param[key]
				}
			})
		}

		// #ifdef H5
		if (envArr.includes('NativeApp')) {
			let liveArr = ['/livePages/live/live-preview-details', '/livePages/live/live']
			// 切换tab 更换调用方法
			let tab = tabbarList.indexOf(to)
			if (tab != -1) {
				console.log('进来了 tbanananna---', tab)
				sdkMethods.jumpToTab(tab)
				return
			}
			//  跳转登录
			if (param && param.app) {
				sdkMethods.jumpLogin()
				return
			}
			// 如果是直播间链接
			if (liveArr.indexOf(to) != -1) {
				sdkMethods.startAudience(param.live_id)
				return
			}

			// 普通跳转
			console.log('进来了饿饿饿饿 ', url)
			sdkMethods.jumpPage(url)
			return
		}
		// #endif

		for (let i = 0; i < tabbarList.length; i++) {
			if (to.indexOf(tabbarList[i]) != -1) {
				uni.switchTab({
					url
				})
				return
			}
		}

		switch (mode) {
		case 'tabbar':
			// 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
			uni.switchTab({
				url
			})
			break
		case 'redirectTo':
			// 关闭当前页面，跳转到应用内的某个页面。
			uni.redirectTo({
				url
			})
			break
		case 'reLaunch':
			// 关闭所有页面，打开到应用内的某个页面。
			uni.reLaunch({
				url
			})
			break
		default:
			// 保留当前页面，跳转到应用内的某个页面
			uni.navigateTo({
				url
			})
		}
	},
	// 获取参数以Object方式返回
	getCurrentAllPages() {
		let pages = getCurrentPages()
		let len = pages.length
		let curParam = pages[len - 1].options // 获取当前页面参数
		let url = '/' + pages[len - 1].route
		if (Object.keys(curParam).length != 0) {
			let param = []
			for (let key in curParam) { // 获取key=value键值对格式数组
				param.push(key + '=' + curParam[key])
			}
			let _url = '' // 除去第一个参数拼接后面参数
			param.forEach((item, i) => {
				if (i != 0) { // 拼接&符号，由于第一组前拼接的是？所有第一组需要单独处理
					_url += '&' + item
				}
			})
			url = url + '?' + param[0] + _url
		}
		return url
	},
	// 获取参数以Object方式返回
	queryObj() {
		let pages = getCurrentPages()
		let options = pages[pages.length - 1].options
		if (options != undefined) {
			return options
		}
		return {}
	},
	/**
     * 图片路径转换
     * @param {String} img_path 图片地址
     * @param {Object} params 参数，针对商品、相册里面的图片区分大中小，size: big、mid、small
     */
	img(img_path, params) {
		var path = ''
		if (img_path && img_path != undefined && img_path != '') {
			if (params && img_path != this.getDefaultImage().default_goods_img) {
				// 过滤默认图
				let arr = img_path.split('.')
				let suffix = arr[arr.length - 1]
				arr.pop()
				arr[arr.length - 1] = arr[arr.length - 1] + '_' + params.size
				arr.push(suffix)
				img_path = arr.join('.')
			}
			if (img_path.indexOf('http://') == -1 && img_path.indexOf('https://') == -1) {
				path = Config.imgDomain + '/' + img_path
			} else {
				path = img_path
			}
		}
		return path
	},
	/**
 * 图片路径转换
 * @param {String} img_path 图片地址
 * @param {Object} params 参数，针对商品、相册里面的图片区分大中小，size: big、mid、small
 */
	imgPath(img_path, params) {
		var path = ''
		if (img_path && img_path != undefined && img_path != '') {
			if (params && img_path != this.getDefaultImage().default_goods_img) {
				// 过滤默认图
				let arr = img_path.split('.')
				let suffix = arr[arr.length - 1]
				arr.pop()
				arr[arr.length - 1] = arr[arr.length - 1] + '_' + params.size
				arr.push(suffix)
				img_path = arr.join('.')
			}
			if (img_path.indexOf('http://') == -1 && img_path.indexOf('https://') == -1) {
				path = Config.imgPath + img_path
			} else {
				path = img_path
			}
		}
		return path
	},
	/**
     * 时间戳转日期格式
     * @param {Object} timeStamp
     */
	timeStampTurnTime(timeStamp, type = '') {
		if (timeStamp != undefined && timeStamp != '' && timeStamp > 0) {
			var date = new Date()
			date.setTime(timeStamp * 1000)
			var y = date.getFullYear()
			var m = date.getMonth() + 1
			m = m < 10 ? '0' + m : m
			var d = date.getDate()
			d = d < 10 ? '0' + d : d
			var h = date.getHours()
			h = h < 10 ? '0' + h : h
			var minute = date.getMinutes()
			var second = date.getSeconds()
			minute = minute < 10 ? '0' + minute : minute
			second = second < 10 ? '0' + second : second
			if (type) {
				return y + '-' + m + '-' + d
			}
			return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second

		}
		return ''

	},
	/**
     * 日期格式转时间戳
     * @param {Object} timeStamp
     */
	timeTurnTimeStamp(string) {
		var f = string.split(' ', 2)
		var d = (f[0] ? f[0] : '').split('-', 3)
		var t = (f[1] ? f[1] : '').split(':', 3)
		return new Date(
			parseInt(d[0], 10) || null,
			(parseInt(d[1], 10) || 1) - 1,
			parseInt(d[2], 10) || null,
			parseInt(t[0], 10) || null,
			parseInt(t[1], 10) || null,
			parseInt(t[2], 10) || null
		).getTime() / 1000
	},
	/**
     * 倒计时
     * @param {Object} seconds 秒
     */
	countDown(seconds) {
		let [day, hour, minute, second] = [0, 0, 0, 0]
		if (seconds > 0) {
			day = Math.floor(seconds / (60 * 60 * 24))
			hour = Math.floor(seconds / (60 * 60)) - day * 24
			minute = Math.floor(seconds / 60) - day * 24 * 60 - hour * 60
			second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60
		}
		if (day < 10) {
			day = '0' + day
		}
		if (hour < 10) {
			hour = '0' + hour
		}
		if (minute < 10) {
			minute = '0' + minute
		}
		if (second < 10) {
			second = '0' + second
		}
		return {
			d: day,
			h: hour,
			i: minute,
			s: second
		}
	},
	/**
     * 数值去重
     * @param {Array} arr 数组
     * @param {string} field 字段
     */
	unique(arr, field) {
		const res = new Map()
		return arr.filter((a) => !res.has(a[field]) && res.set(a[field], 1))
	},
	/**
     * 判断值是否在数组中
     * @param {Object} elem
     * @param {Object} arr
     * @param {Object} i
     */
	inArray(elem, arr) {
		return arr == null ? -1 : arr.indexOf(elem)
	},
	/**
     * 获取某天日期
     * @param {Object} day
     */
	getDay(day) {
		var today = new Date()
		var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
		today.setTime(targetday_milliseconds)

		const doHandleMonth = function (month) {
			var m = month
			if (month.toString().length == 1) {
				m = '0' + month
			}
			return m
		}

		var tYear = today.getFullYear()
		var tMonth = today.getMonth()
		var tDate = today.getDate()
		var tWeek = today.getDay()
		var time = parseInt(today.getTime() / 1000)
		tMonth = doHandleMonth(tMonth + 1)
		tDate = doHandleMonth(tDate)

		const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
		return {
			't': time,
			'y': tYear,
			'm': tMonth,
			'd': tDate,
			'w': week[tWeek]
		}
	},
	/**
     * 图片选择加上传
     * @param number num
     * @param {Object} params
     * @param {Object} callback
     * @param string url
     * return array
     */
	upload(num, params, callback, url) {
		// #ifdef H5
		var app_type = this.isWeiXin() ? 'wechat' : 'h5'
		var app_type_name = this.isWeiXin() ? '微信公众号' : 'H5'
		// #endif

		// #ifdef MP-WEIXIN
		var app_type = 'weapp'
		var app_type_name = '微信小程序'
		// #endif

		// #ifdef MP-ALIPAY
		var app_type = 'aliapp'
		var app_type_name = '支付宝小程序'
		// #endif

		// #ifdef MP-BAIDU
		var app_type = 'baiduapp'
		var app_type_name = '百度小程序'
		// #endif

		// #ifdef MP-TOUTIAO
		var app_type = 'MP-TOUTIAO'
		var app_type_name = '头条小程序'
		// #endif

		// #ifdef MP-QQ
		var app_type = 'MP-QQ'
		var app_type_name = 'QQ小程序'
		// #endif
		var data = {
			token: uni.getStorageSync('token'),
			app_type,
			app_type_name
		}
		data = Object.assign(data, params)

		// 请求参数加密
		// if (Config.apiSecurity) {
		// 	let jsencrypt = new JSEncrypt();
		// 	jsencrypt.setPublicKey(Config.publicKey);
		// 	let encrypt = encodeURIComponent(jsencrypt.encryptLong(JSON.stringify(data)));
		// 	data = {
		// 		encrypt
		// 	};
		// }
		var imgs_num = num
		var _self = this
		uni.chooseImage({
			count: imgs_num,
			sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 从相册或者拍照
			async success(res) {
				const tempFilePaths = res.tempFilePaths

				var _data = data
				var imgs = []
				for (var i = 0; i < tempFilePaths.length; i++) {
					var path = await _self.upload_file_server(tempFilePaths[i], _data, params.path, url)
					imgs.push(path)
				}
				typeof callback == 'function' && callback(imgs)
			}
		})
	},
	// 上传
	upload_file_server(tempFilePath, data, path, url = '') {
		if (url) {
			var uploadUrl = Config.baseUrl + url
		} else {
			var uploadUrl = Config.baseUrl + '/api/upload/' + path
		}
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: uploadUrl,
				filePath: tempFilePath,
				name: 'file',
				formData: data,
				success(res) {
					var path_str = JSON.parse(res.data)
					if (path_str.code >= 0) {
						resolve(path_str.data.pic_path)
					} else {
						reject('error')
					}
				}
			})

		})

	},
	/**
     * 复制
     * @param {Object} message
     * @param {Object} callback
     */
	copy(value, callback) {
		// #ifdef H5
		var oInput = document.createElement('input') // 创建一个隐藏input（重要！）
		oInput.value = value // 赋值
		document.body.appendChild(oInput)
		oInput.select() // 选择对象
		document.execCommand('Copy') // 执行浏览器复制命令
		oInput.className = 'oInput'
		oInput.style.display = 'none'
		uni.hideKeyboard()
		this.showToast({
			title: '复制成功'
		})

		typeof callback == 'function' && callback()
		// #endif

		// #ifdef MP || APP-PLUS
		uni.setClipboardData({
			data: value,
			success: () => {
				typeof callback == 'function' && callback()
			}
		})
		// #endif
	},
	/**
     * 是否是微信浏览器
     */
	isWeiXin() {
		// #ifndef H5
		return false
		// #endif
		var ua = navigator.userAgent.toLowerCase()
		if (ua.match(/MicroMessenger/i) == 'micromessenger') {
			return true
		}
		return false

	},
	/**
     * 显示消息提示框
     *  @param {Object} params 参数
     */
	showToast(params = {}) {
		params.title = params.title || ''
		params.icon = params.icon || 'none'
		params.position = params.position || 'bottom'
		params.duration = 1500
		uni.showToast(params)
	},
	/**
     * 检测是否刘海屏手机
     */
	isPhoneBang() {
		let res = uni.getSystemInfoSync()
		console.log(res, 'reerer')
		if (res.statusBarHeight >= 44) {
			return true
		}
		return false
	},
	/**
     * 检测苹果X以上的手机
     */
	isIPhoneX() {
		let res = uni.getSystemInfoSync()
		if (res.model.search('iPhone X') != -1) {
			return true
		}
		return false
	},
	// 判断安卓还是iOS
	isAndroid() {
		let platform = uni.getSystemInfoSync().platform
		if (platform == 'ios') {
			return false
		} else if (platform == 'android') {
			return true
		}
	},
	/**
     * 深度拷贝对象
     * @param {Object} obj
     */
	deepClone(obj) {
		const isObject = function (obj) {
			return typeof obj == 'object'
		}

		if (!isObject(obj)) {
			throw new Error('obj 不是一个对象！')
		}
		// 判断传进来的是对象还是数组
		let isArray = Array.isArray(obj)
		let cloneObj = isArray ? [] : {}
		// 通过for...in来拷贝
		for (let key in obj) {
			cloneObj[key] = isObject(obj[key]) ? this.deepClone(obj[key]) : obj[key]
		}
		return cloneObj
	},
	refreshBottomNav() {
		var bottomNav = uni.getStorageSync('bottom_nav')
		bottomNav = JSON.parse(bottomNav)
		for (var i = 0; i < bottomNav.list.length; i++) {
			var item = bottomNav.list[i]
			var obj = {
				index: i
			}
			obj.text = item.title
			obj.iconPath = this.img(item.iconPath)
			obj.selectedIconPath = this.img(item.selectedIconPath)
			if (bottomNav.type == 1) {
				// 图文
			} else if (bottomNav.type == 2) {
				// 图片
			} else if (bottomNav.type == 3) {
				// 文字
			}
			uni.setTabBarItem(obj)
		}
	},
	/**
     * 自定义模板的跳转链接
     * @param {Object} link
     */
	diyRedirectTo(link, method) {
		if (link == null || link == '' || !link.wap_url) return
		if (link.wap_url.indexOf('http') != -1) {
			this.redirectTo('/otherpages/webview/webview', {
				link: encodeURIComponent(link.wap_url)
			})
		} else {
			var params = link.site_id ? {
				site_id: link.site_id
			} : {}
			this.redirectTo(link.wap_url)
		}
	},
	/**
     * 获取默认图
     * @param {Object} link
     */
	getDefaultImage() {
		let defaultImg = uni.getStorageSync('default_img')
		if (defaultImg) {
			defaultImg = JSON.parse(defaultImg)
			defaultImg.default_goods_img = this.img(defaultImg.default_goods_img)
			defaultImg.default_headimg = this.img(defaultImg.default_headimg)
			defaultImg.default_shop_img = this.img(defaultImg.default_shop_img)
			defaultImg.default_category_img = this.img(defaultImg.default_category_img)
			defaultImg.default_city_img = this.img(defaultImg.default_city_img)
			defaultImg.default_supply_img = this.img(defaultImg.default_supply_img)
			defaultImg.default_store_img = this.img(defaultImg.default_store_img)
			defaultImg.default_brand_img = this.img(defaultImg.default_brand_img)
			return defaultImg
		}
		return {
			default_goods_img: '',
			default_headimg: '',
			default_shop_img: '',
			default_category_img: '',
			default_city_img: '',
			default_supply_img: '',
			default_store_img: '',
			default_brand_img: ''
		}

	},
	/**
     * 判断手机是否为iphoneX系列
     */
	uniappIsIPhoneX() {
		let isIphoneX = false
		let systemInfo = uni.getSystemInfoSync()
		// #ifdef MP
		if (systemInfo.model.search('iPhone X') != -1 || systemInfo.model.search('iPhone 11') != -1 || systemInfo.model.search(
			'iPhone12') != -1) {
			isIphoneX = true
		}
		// #endif

		// #ifdef H5
		var u = navigator.userAgent
		var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端

		if (isIOS) {
			if (systemInfo.screenWidth == 375 && systemInfo.screenHeight == 812 && systemInfo.pixelRatio == 3) {
				isIphoneX = true
			} else if (systemInfo.screenWidth == 414 && systemInfo.screenHeight == 896 && systemInfo.pixelRatio == 3) {
				isIphoneX = true
			} else if (systemInfo.screenWidth == 414 && systemInfo.screenHeight == 896 && systemInfo.pixelRatio == 2) {
				isIphoneX = true
			}
		}
		// #endif
		return isIphoneX
	},
	/**
     * 判断手机是否为iphone11系列
     */
	uniappIsIPhone11() {
		let isIphone11 = false
		let systemInfo = uni.getSystemInfoSync()
		// #ifdef MP
		if (systemInfo.model.search('iPhone 11') != -1) {
			isIphone11 = true
		}
		// #endif
		return isIphone11
	},
	/**
     * 判断当前页面栈是否存在，如果存在，则返回该页面栈，否则跳转到该页面
     * @param {Object} url
     */
	jumpPage(url) {
		let jump = true
		let arr = getCurrentPages().reverse()
		for (let i = 0; i < arr.length; i++) {
			if (url.indexOf(arr[i].route) != -1) {
				jump = false
				uni.navigateBack({
					delta: i
				})
				break
			}
		}
		if (jump) {
			this.$util.diyRedirectTo(url)
		}
	},
	/**
     * 计算两个经纬度之间的距离
     * @param {Object} lat1
     * @param {Object} lng1
     * @param {Object} lat2
     * @param {Object} lng2
     */
	getDistance(lat1, lng1, lat2, lng2) {
		var radLat1 = lat1 * Math.PI / 180.0
		var radLat2 = lat2 * Math.PI / 180.0
		var a = radLat1 - radLat2
		var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0
		var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
            Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)))
		s = s * 6378.137 // EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000
		return s
	},
	// #ifdef H5
	/**
     * 判断该浏览器是否为safaria浏览器
     */
	isSafari() {
		let res = uni.getSystemInfoSync()
		var ua = navigator.userAgent.toLowerCase()
		if (ua.indexOf('applewebkit') > -1 && ua.indexOf('mobile') > -1 && ua.indexOf('safari') > -1 &&
            ua.indexOf('linux') === -1 && ua.indexOf('android') === -1 && ua.indexOf('chrome') === -1 &&
            ua.indexOf('ios') === -1 && ua.indexOf('browser') === -1) {
			return true
		}
		return false

	},
	// #endif
	goBack(backUrl = '/pages/index/index/index') {

		// #ifdef H5
		if (envArr.includes('NativeApp')) {
			sdkMethods.exit()
			return
		}
		// #endif

		if (getCurrentPages().length == 1) {
			this.redirectTo(backUrl)
		} else {
			uni.navigateBack()
		}
	},
	/**
     * 转换数字，保留f位
     * @param {Object} e
     * @param {Object} f
     */
	numberFixed(e, f) {
		if (!f) {
			f = 0
		}
		return Number(e).toFixed(f)
	},
	/**
     * 获取url参数
     * @param {Object} callback
     */
	getUrlCode(callback) {
		var url = location.search
		var theRequest = new Object()
		if (url.indexOf('?') != -1) {
			var str = url.substr(1)
			var strs = str.split('&')
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
			}
		}
		typeof callback == 'function' && callback(theRequest)
	},
	// 站内信时间封装
	getSitemessageDate(time) {
		let curr_time = new Date()

		let curr_year = Number(curr_time.getFullYear())
		let curr_month = Number(curr_time.getMonth()) + 1
		let curr_day = Number(curr_time.getDate())
		let curr_hours = Number(curr_time.getHours())
		let curr_minutes = Number(curr_time.getMinutes())

		let arr = time.split(' ')

		let send_year = Number(arr[0].split('-')[0])
		let send_month = Number(arr[0].split('-')[1])
		let send_day = Number(arr[0].split('-')[2])
		let send_hours = Number(arr[1].split(':')[0])
		let send_minutes = Number(arr[1].split(':')[1])
		let send_date = send_year + '-' + (send_month >= 10 ? send_month : '0' + send_month) + '-' + (send_day >= 10 ? send_day : '0' + send_day)

		if (curr_year > send_year) {
			return send_date
		}
		if (curr_month > send_month || curr_month == send_month && curr_day - send_day > 1) {
			return (send_month >= 10 ? send_month : '0' + send_month) + '-' + (send_day >= 10 ? send_day : '0' + send_day)
		} else if (curr_month == send_month && curr_day - send_day == 1) {
			return '昨天' + (send_hours >= 10 ? send_hours : '0' + send_hours) + ':' + (send_minutes >= 10 ? send_minutes : '0' + send_minutes)
		}
		return (send_hours >= 10 ? send_hours : '0' + send_hours) + ':' + (send_minutes >= 10 ? send_minutes : '0' + send_minutes)

	},

	// 一期公共方法

	// 获取参数以Object方式返回
	queryObj() {
		let pages = getCurrentPages()
		let options = pages[pages.length - 1].options
		if (options != undefined) {
			return options
		}
		return {}

	},
	// 自定义转时间
	formatDateCount(num, format) {
		const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
		const returnArr = []
		const date = new Date(num)
		returnArr.push(date.getFullYear()) // 取得4位数的年份
		returnArr.push(this.formatNumberCount(date.getMonth() + 1)) // 取得日期中的月份，其中0表示1月，11表示12月
		returnArr.push(this.formatNumberCount(date.getDate())) // 返回日期月份中的天数（1到31）
		returnArr.push(this.formatNumberCount(date.getHours())) // 返回日期中的小时数（0到23）
		returnArr.push(this.formatNumberCount(date.getMinutes())) // 返回日期中的分钟数（0到59）
		returnArr.push(this.formatNumberCount(date.getSeconds())) // 返回日期中的秒数（0到59）

		for (const i in returnArr) {
			// 判断对象是否含有某个非继承属性
			if ({}.hasOwnProperty.call(returnArr, i)) {
				format = format.replace(formateArr[i], returnArr[i]) // 替换
			}
		}
		return format
	},
	formatNumberCount(n) {
		n = n.toString()
		return n[1] ? n : `0${n}`
	},
	/* 判断url是否为绝对路径 */
	posUrl(url) {
		return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
	},
	// 获取位置
	getLocation() {
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'wgs84',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					uni.showToast({
						title: '位置获取失败',
						duration: 3000,
						icon: 'none' // success，loading
					})
					reject(err)
				}
			})
		})
	},
	/**
     * 表单验证提示 无按钮
     * @param {title} 提示文本信息
     * @param {duration} 延迟时间
     * icon设置为none后，一行最多容纳11字
     * h5多行，小程序两行，也就是22字
     */
	tip(title = '', duration = 1500) {
		uni.showToast({
			title,
			duration,
			icon: 'none' // success，loading
		})
	},
	/**
     * 需要跟原生交互的时候路径跳转 --- 兼容性跳转路径
     *参数appobj：{key:$sdk定义的key,query:app需要的参数非比传}，inx内部回退的方法---页面层数
     */
	compatiBleJump(appobj, inx) {
		// #ifdef H5
		if (envArr.includes('NativeApp')) {
			// 判断是否需要参数
			if (appobj.query) {
				sdkMethods[appobj.key](appobj.query)
			} else {
				sdkMethods[appobj.key]()
			}
		} else {
			let pagesLen = getCurrentPages().length
			if (pagesLen == 1) {
				uni.reLaunch({
					url: '/pages/index/index/index'
				})
			} else {
				this.back(inx)
			}
		}
		// #endif
		// #ifdef MP-WEIXIN
		let pagesLen = getCurrentPages().length
		if (pagesLen == 1) {
			uni.reLaunch({
				url: '/pages/index/index'
			})
		} else {
			this.back(inx)
		}
		// #endif
	},
	// 建议是否登录了
	isLoginCall() {
		let token = uni.getStorageSync('token') || ''
		// console.log('log', $store.getters.userInfo.headimg, 66666);
		// return false
		if (token) {
			return true
		}
		// #ifdef H5
		if (envArr.includes('NativeApp')) {
			sdkMethods.jumpLogin()
		} else {
			$store.commit('SET_SHOUQUAN', true)
		}
		// #endif

		// #ifdef MP-WEIXIN
		$store.commit('SET_SHOUQUAN', true)
		// #endif
	},
	/**
     * 跳转登录
     */
	jumpLogin() {
		// #ifdef H5
		if (envArr.includes('NativeApp')) {
			sdkMethods.jumpLogin()
		} else {
			let currentPage = getCurrentPages()[getCurrentPages().length - 1]
			let currentRoute = currentPage.route
			console.log(currentRoute, 'pagespagespages')
			console.log(this.queryObj(), 'queryObjqueryObj')
			let url = '/' + currentRoute
			let params = this.queryObj()
			for (var o in params) {
				// if (url.includes('?'))
				if (!url.includes('?')) {
					url = `${url}?${o}=` + params[o]
				} else {
					url = `${url}&${o}=` + params[o]
				}
			}
			if (url) {
				this.redirectTo('/pages/login/login/login', { back: encodeURIComponent(url) })
			} else {
				this.redirectTo('/pages/login/login/login')
			}
		}
		// #endif

		// #ifdef MP-WEIXIN
		$store.commit('SET_SHOUQUAN', true)
		// #endif

		window.loginLock = false

	},
	// 海报转换
	imgChanges(img_path) {
		var path = ''
		if (img_path == '') {
			path = ''
		} else if (img_path.indexOf('http://') == -1 && img_path.indexOf('https://') == -1) {
			path = BASEURL + img_path
		} else {
			path = img_path
		}
		return path
	},
	/**
     * 复制文本
     * @param {String} text 复制的内容
     */
	copyText(text) {
		return new Promise((resolve, reject) => {
			// console.log('log', text);
			if (typeof text == 'string') {
				// #ifdef MP-WEIXIN
				uni.setClipboardData({
					data: text,
					success: () => {
						resolve({
							code: 0,
							data: '复制成功(#^.^#)'
						})
					},
					fail: (err) => {
						uni.showToast({
							title: '复制失败了┭┮﹏┭┮',
							duration: 3000,
							icon: 'none'
						})
						reject(err)
					}
				})
				// #endif
				// #ifdef H5
				let newType = h5Copy(text)
				if (newType) {
					resolve({
						code: 0,
						data: '复制成功(#^.^#)'
					})
				} else {
					uni.showToast({
						title: '复制失败了┭┮﹏┭┮',
						duration: 3000,
						icon: 'none'
					})
					resolve({
						code: -1,
						data: '复制失败了┭┮﹏┭┮'
					})
				}
				// #endif
			} else {
				reject('Please pass in a string 请传入字符串')
			}
		})
	},
	// 返回上一页
	back(delta = 1) {
		var len = getCurrentPages().length
		console.log(len, 'lenlenlenlen')
		if (len > 1) {
			uni.navigateBack({
				delta
			})
		} else {
			uni.reLaunch({
				url: '/pages/index/index/index'
			})
		}
	},
	/**
     * 毫秒转换友好的显示格式
     * 输出格式：21小时前
     * @param  {[type]} time [description]
     * @return {[type]}      [description]
     */
	dateStr(timespan = '', type = true) {
		let dateTime = null
		if (timespan) {
			dateTime = new Date(timespan)
		} else {
			dateTime = new Date()
		}

		let year = dateTime.getFullYear()
		let month = dateTime.getMonth() + 1
		let day = dateTime.getDate()
		let hour = dateTime.getHours()
		let minute = dateTime.getMinutes()
		let second = dateTime.getSeconds()
		let now = new Date()
		let now_new = Date.parse(now.toDateString()) // typescript转换写法
		let milliseconds = 0
		let timeSpanStr
		if (type) {
			milliseconds = now_new - timespan

			if (milliseconds <= 1000 * 60 * 1) {
				timeSpanStr = '刚刚'
			} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
				timeSpanStr = Math.round(milliseconds / (1000 * 60)) + '分钟前'
			} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
				timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前'
			} else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
				timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前'
			} else {
				timeSpanStr = year + '年' + month + '月' + day + '日'
			}
		} else {
			let newMonth = month < 10 ? '0' + month : month
			let newDay = day < 10 ? '0' + day : day
			timeSpanStr = year + '/' + newMonth + '/' + newDay
		}
		return timeSpanStr
	},
	// 正则校验集合
	regCheckAll: {
		/**
         * 校验手机号
         * mobile 手机号
         */
		isPhone(mobile) {
			var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
			if (!myreg.test(mobile)) {
				return false
			}
			return true

		}
	},
	// 自定义转时间
	formatDateCount(num, format) {
		const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
		const returnArr = []
		const date = new Date(num)
		returnArr.push(date.getFullYear()) // 取得4位数的年份
		returnArr.push(this.formatNumberCount(date.getMonth() + 1)) // 取得日期中的月份，其中0表示1月，11表示12月
		returnArr.push(this.formatNumberCount(date.getDate())) // 返回日期月份中的天数（1到31）
		returnArr.push(this.formatNumberCount(date.getHours())) // 返回日期中的小时数（0到23）
		returnArr.push(this.formatNumberCount(date.getMinutes())) // 返回日期中的分钟数（0到59）
		returnArr.push(this.formatNumberCount(date.getSeconds())) // 返回日期中的秒数（0到59）

		for (const i in returnArr) {
			// 判断对象是否含有某个非继承属性
			if ({}.hasOwnProperty.call(returnArr, i)) {
				format = format.replace(formateArr[i], returnArr[i]) // 替换
			}
		}
		return format
	},
	// 时间戳转成日期
	formatDate(time, type = true, icon = '-') {
		if (time != null && time != '') {
			let date = new Date(time)
			let Year = date.getFullYear() // 年份
			let Month = date.getMonth() >= 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1) // 月份，注意：js里的月要加1
			let Day = date.getDate() >= 9 ? date.getDate() : '0' + date.getDate() // 日
			let hour = date.getHours() >= 9 ? date.getHours() : '0' + date.getHours()
			let minute = date.getMinutes() >= 9 ? date.getMinutes() : '0' + date.getMinutes()
			let second = date.getSeconds() >= 9 ? date.getSeconds() : '0' + date.getSeconds()
			let CurrentDate = ''
			if (type) {
				return `${Year}${icon}${Month}${icon}${Day}`
			}
			return `${Year}${icon}${Month}${icon}${Day} ${hour}:${minute}:${second}`

		}
	},
	/**
     * 定制化消息提示弹框 有按钮
     * @param {content} 提示信息
     * @param {url} 跳转地址，-1为返回上一页。
     */
	msg(content, url = null, type = 'push', showCancel = false, callback) {
		var _url = url
		uni.showModal({
			title: '温馨提醒',
			content,
			// confirmText: confirmText, // 确定按钮字 （ 确定取消最多 4 个字符
			// confirmColor: confirmColor, // 确定按钮字体颜色
			// cancelText: cancelText, // 取消按钮字
			showCancel, // 是否显示取消按钮
			success(res) {
				// console.log(_url)
				// if (res.confirm) {
				// 	if (callback) {
				// 		callback()
				// 	}
				// }
				// if (_url != null) {
				// 	if (_url === -1) {
				// 		$mRouter.back(1);
				// 	} else {
				// 		$mRouter[type](_url);
				// 	}
				// }
			} // 成功回调
		})
	},
	/*
     * 不显示打印数据
     */
	eliminateLogAll() {
		console.log = function () { }
	}

}

